#-*-coding:utf-8-*-
'''
Created on 2009/6/7

@author: chenhh
'''

import BeautifulSoup
import string
import re
import cStringIO

def deleteComma(str):
    list = str.split(',')
    return string.join(list, '')


if __name__ == '__main__':
    stock_ids = (2330,)
    output = open('/tmp/result.txt', 'w')
    
    
    for id in stock_ids:
        for year in xrange(2007, 2009):
            for month in xrange(1, 13):
                formatedMon = string.zfill(month, 2)
                file = open('/tmp/%s_%s%s.htm'%(id, year, formatedMon)).read()
                outString = cStringIO.StringIO()
                #beautiful soup is the bottleneck
                soup = BeautifulSoup.BeautifulSoup(file)    
                table = soup.findAll('tr', {'class':'basic2'})
                
                #parse data from table
                for i, tr in enumerate(table):
                    #跳過標題欄
                    if(i == 0):
                        continue
                    result = []
                    for element in tr.contents:
                       if(element.string is None):
                           result.append(element.div.string)
                       else:
                           result.append(deleteComma(element.string))

                    out_str = "%d %s"%(id, string.join(result))
                    print >>outString, out_str
                
                output.write(outString.getvalue())
                outString.close()
                print "%s_%s%s complete"%(id, year, formatedMon)
        
    output.close()